package com.webapps.tvprogram.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.webapps.tvprogram.R;
import com.webapps.tvprogram.data.local.Program;
import com.webapps.tvprogram.data.local.ProgramItem;
import com.webapps.tvprogram.utils.GlobalInfo;

public class SingleProgramList extends Activity {

    private List<Map<String, Object>> list;
    private SimpleAdapter adapter;
    private static final String DATE = "date";
    private static final String TIME = "time";
    private static final String NAME = "name";
    private static final String TYPE = "type";
    private static final String[] PARAM = { DATE, TIME, NAME, TYPE };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String programName = getIntent().getExtras().getString(GlobalInfo.BUNDLE_PROGRAM_NAME);
        list = new ArrayList<Map<String, Object>>();
        setContentView(R.layout.program_list_view_single);
        final List<Program> progsList = MainActivity.getInstance().getProgramsData();
        Program p = null;
        for (Iterator<Program> iterator = progsList.iterator(); iterator.hasNext();) {
            p = iterator.next();
            if (p.getName().equalsIgnoreCase(programName))
                break;
        }
        if (p == null) {
            // TODO announce error
            return;
        }
        for (Iterator<ProgramItem> iterator = p.getProgramItems().iterator(); iterator.hasNext();) {
            ProgramItem pi = iterator.next();
            addItem(list, countProgramDate(pi), countProgramItemTime(pi), pi.getName(), pi.getType());
        }
        ImageView iv = (ImageView) findViewById(R.id.programs_list_single_icon);
        TextView tv = (TextView) findViewById(R.id.programs_list_single_prog_name);
        if (p.getIcon() != null)
            iv.setImageBitmap(p.getIcon());
        else
            iv.setImageResource(R.drawable.icon);
        tv.setText(p.getName());
        adapter = new SimpleAdapter(this, list, R.layout.list_4text_elem_single_program, PARAM, new int[] {
                R.id.text_date, R.id.text_time, R.id.text_prog_name, R.id.text_prog_type });
        ListView lv = (ListView) findViewById(R.id.programs_list_single);
        lv.setAdapter(adapter);
    }

    private void addItem(List<Map<String, Object>> data, String date, String time, String name, String type) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(DATE, date);
        map.put(TIME, time);
        map.put(NAME, name);
        map.put(TYPE, type);
        data.add(map);
    }

    private String countProgramItemTime(ProgramItem item) {
        Date startDate = item.getStartTime();
        // long startTime = startDate.getTime();
        // startTime += (long) item.getLength() * 60 * 1000;
        // Date endDate = new Date(startTime);
        String format = "HH:mm";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(startDate);// + " - " + sdf.format(endDate);
    }

    private String countProgramDate(ProgramItem item) {
        Date startDate = item.getStartTime();
        String format = "dd MMMM";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(startDate);
    }
}
